package com.dcy.znoa.controller.estate;

import com.aliyuncs.exceptions.ClientException;
import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.estate.model.outnote.AddEstateOutnoteModel;
import com.dcy.znoa.controller.estate.model.outnote.SelectEstateOutnoteModel;
import com.dcy.znoa.controller.estate.model.outnote.UpdateEstateOutnoteModel;
import com.dcy.znoa.domain.estate.EstateOutnote;
import com.dcy.znoa.service.estate.EstateOutnoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import static com.dcy.znoa.common.util.pojo.JsonCommon.INSERT_ERROR_CODE;
import static com.dcy.znoa.common.util.pojo.JsonCommon.SUCCESS_CODE;
import static com.dcy.znoa.common.util.pojo.JsonCommon.UPDATE_ERROR_CODE;

/**
 * <p>
 * 物业出门条 前端控制器
 * </p>
 *
 * @author cook
 * @since 2018-05-21op
 */
@Slf4j
@Controller
@RequestMapping("/estateOutnote")
public class EstateOutnoteController extends BaseController {

    @Autowired
    EstateOutnoteService eoService;
    //列表查询 分页  通用Controller
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectEstateOutnoteModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonForList("404", bindingResult.getFieldError().getDefaultMessage());
        }
        return eoService.selectList(model);
    }


    //单条数据查询 通用Controller
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectById")
    public JsonCommon selectById(Long esonId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (esonId == null) {
            return new JsonCommon("404", "esonId不能为空");
        }
        return  new JsonCommon(eoService.selectByEsserId(esonId));
    }


    //APP
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddEstateOutnoteModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException, ClientException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        EstateOutnote cp = new EstateOutnote();
        log.info("model.getRemoteFileName() = " + model.getRemoteFileName());
        BeanUtils.copyProperties(cp, model);
        log.info("cp.getRemoteFileName() = " + cp.getRemoteFileName());
        cp.setSendOperId(getCurrentUser().getOperId());
        cp.setCreateOperId(getCurrentUser().getOperId());
        cp.setCompId(getCurrentUser().getCompId());
        cp.setCreateTime(new Date());
        cp.setValidFlag(1);
        boolean result = eoService.add(cp);
        if(result){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(INSERT_ERROR_CODE);
        }
    }
    //通用
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateEstateOutnoteModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException, ClientException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Integer status = model.getStatus();
        if(!Integer.valueOf(3).equals(status) && model.getEsonId()==null){
            return  new JsonCommon("404","非扫码时，esonId不能为空");
        }
        boolean result = eoService.update(model);
        if(result){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(UPDATE_ERROR_CODE);
        }
    }

}
